import configparser
import os.path
from selenium import webdriver
from framework.logger import Logger

logger = Logger(logger='BrowserEngine').getlog()

class BrowserEngine(object):

    dir = os.path.dirname(os.path.dirname(__file__))
    chrome_driver_path = dir + '/tools/chromedriver'

    def __init__(self,driver):
        self.driver = driver

    def open_browser(self,driver):
        dir = os.path.dirname(os.path.dirname(__file__))
        config = configparser.ConfigParser()
        file_path = dir + '/config/config.ini'
        config.read(file_path)

        browser = config.get('browserType','browserName')
        url = config.get('testServer','URL')

        #无痕模式
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('–incognito')

        if browser == 'Firefox':
            driver = webdriver.Firefox()
        elif browser == 'Chrome':
            driver = webdriver.Chrome(self.chrome_driver_path, chrome_options=chrome_options)
        elif browser == 'IE':
            driver = webdriver.Ie()

        driver.get(url)
        driver.maximize_window()
        driver.implicitly_wait(5)

        return driver




